package sell;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;


/**
 * Created by webrx on 2017/6/9.
 */
@WebServlet("/yanzheng")
public class YanZhengMa extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       /* resp.setCharacterEncoding("utf-8");*/
        resp.setContentType("image/jpeg;charset=utf-8");
       /* req.setCharacterEncoding("utf-8");*/
        int w=100;
        int h=40;
        BufferedImage i = new BufferedImage(w,h ,1);
        Graphics2D g = (Graphics2D) i.getGraphics();
        g.setColor(new Color(249,249,200));
        g.fillRect(0,0,w,h );

        String str ="abcdefghigklmnpqrestuvwxyz123456789";
        Random r = new Random();
        g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),r.nextInt(55)+200));
        String cs="";
        for(int j=0;j<4;j++){
            int spn= r.nextInt(str.length());
            String s=str.substring(spn,spn+1);
            cs+=s;
            g.setFont(new Font("宋体",Font.BOLD,r.nextInt(5)+20));
            int x =j*25+5;
            int y=r.nextInt(5)+25;
            g.drawString(s,x,y);

        }
        HttpSession session= req.getSession();
        session.setAttribute("check",cs);
        ImageIO.write(i,"jpeg",resp.getOutputStream());
    }

}
